package com.example.demo.controller;

import com.example.demo.domain.Teacher;
import com.example.demo.service.TeacherService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
//@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private Gson gson;

//  返回视图
    @GetMapping("view")
    public String getView(){
        return "/teacher/teacherPage";
    }

    @GetMapping("/findAllTeachers")
    public String findAll(){
        List<Teacher> myAll = teacherService.findAll();
        String josnStr = gson.toJson(myAll);
        return josnStr;
    }

    @GetMapping("/{id}")
    public String findByTid(@PathVariable String id){
        Teacher myAll = teacherService.findByTid(id);
        String josnStr = gson.toJson(myAll);
        return josnStr;
    }

    @GetMapping("/findLatestTeacher")
    public String findLatestTeacher(){
        List<Teacher> myAll = teacherService.findLatestTeachers();
        String josnStr = gson.toJson(myAll);
        return josnStr;
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Integer id){
        teacherService.deleteById(id);
    }

}
